import { Injectable } from '@angular/core';
import { _HttpClient } from '@delon/theme';
import { MajorDTO } from '../model/major-dto';
import { Observable } from 'rxjs';
import { QueryOptions } from '../util/model/query-options';
import { ApiPagedData } from '../util/model/api-paged-data';

@Injectable({
  providedIn: 'root',
})
export class MajorService {
  /**
   * API的入口URL
   */
  private static URL = '/api/authority/major';
  constructor(private http: _HttpClient) {}

  /**
   * 查询所有
   */
  getAll(): Observable<Array<MajorDTO>> {
    return this.http.get<Array<MajorDTO>>(`${MajorService.URL}/all`);
  }

  /**
   * 分页查询
   *
   * @param option 分页参数
   * @param name 专业名称
   */
  getOnePage(option: QueryOptions, name?: string): Observable<ApiPagedData<MajorDTO>> {
    const params = {};
    Object.assign(params, option);
    Object.assign(params, name ? { name } : {});
    return this.http.get<ApiPagedData<MajorDTO>>(MajorService.URL, params);
  }

  /**
   * 删除专业
   *
   * @param id 专业id
   */
  deleteById(id: string): Observable<any> {
    return this.http.delete(`${MajorService.URL}/${id}`);
  }

  /**
   * 根据ID查找专业
   *
   * @param id 专业id
   */
  findById(id: string): Observable<MajorDTO> {
    return this.http.get<MajorDTO>(`${MajorService.URL}/${id}`);
  }

  /**
   * 修改专业
   *
   * @param item 新增信息
   * @param id 专业id
   */
  update(item: MajorDTO, id: string): Observable<MajorDTO> {
    return this.http.put<MajorDTO>(`${MajorService.URL}/${id}`, item);
  }

  /**
   * 新建专业
   *
   * @param item 修改信息
   */
  create(item: MajorDTO): Observable<MajorDTO> {
    return this.http.post<MajorDTO>(MajorService.URL, item);
  }

  /**
   * 检查z专业名称是否重复
   *
   * @param name 专业名称
   * @param id 用户id
   */
  checkAccountUnique(name: string, id?: string): Observable<any> {
    const params = { name };
    Object.assign(params, id ? { id } : {});
    return this.http.get<any>(`${MajorService.URL}/name-only`, params);
  }
}
